package org.i1510.wx.utils;


import net.sf.json.JSONObject;

import org.i1510.common.utils.StringUtils;
import org.i1510.wx.bean.Button;
import org.i1510.wx.bean.MainButton;
import org.i1510.wx.bean.Menu;
import org.i1510.wx.bean.SubButton;
import org.i1510.wx.constant.API4GZH;
import org.i1510.wx.po.WxPublic;
import org.i1510.wx.utils.Token.AccessToken;

/**
 * @author Administrator
 *
 */
public class Test {

	/**
	 *@Author Rambo
	 *@Desc：菜单获取 
	 *				粉丝获取
	 *@param args TODO
	 *@Version  Test.java,v 1.1 2014-10-9 下午2:14:38 
	 */
//	public static void main(String[] args) {
//		Test test = new Test();
////		JSONObject jsonObject = test.getWeFansListJson(null, "");
//		JSONObject jsonObject = test.getWeMenuListJson(null, "");
//		JSONObject openIdJson = jsonObject.getJSONObject("menu");
//		if (openIdJson != null) {
//			JSONArray jsonArray = openIdJson.getJSONArray("button");
//			if (jsonArray != null && jsonArray.size() > 0) {
//				for (int i = 0; i < jsonArray.size(); i++) {
//					JSONObject ob = jsonArray.getJSONObject(i);
//					System.out.println(ob.get("name")+"="+ob.get("type"));
//					
//				}
//			}
//		}
//		
//		System.out.println("--->>>");
//
//	}
	public JSONObject getWeMenuListJson(WxPublic wePublic, String nextOpenId) {
		String fdCode = "wx359601ac8fa2a57a"; //wePublic.getQrcode();
		String appId = "wx359601ac8fa2a57a";//wePublic.getAppId();
		String appSecret = "b8b9dccc95af7cbf88cfa16e90c2ceaf";//wePublic.getAppSecret();
//		AccessToken accessToken = WxUtils.getAccessToken4GZH(appId,appSecret);
//		String tokenId = TokenUtils.checkToken(fdCode, appId, appsecret,"test");
		String access_userlist_url = API4GZH.MEMU_LIST_URL;

		// 第一个拉取的OPENID，不填默认从头开始拉取
		String requestUrl = "";
//		access_userlist_url = access_userlist_url.replace("ACCESS_TOKEN",accessToken.getToken());
		access_userlist_url = access_userlist_url.replace("ACCESS_TOKEN",
				"NAVHE00piJbT6XKhcu0Gfh17eAlY7tvhRDgsPrEoppal0GH6xqovSkPWsf4K0wwYJlJmElyco0Ntjm49-4S1VA");
		
		requestUrl = access_userlist_url;
		JSONObject jsonObject = WxUtils.httpRequest(requestUrl, "GET", null);
		return jsonObject;
	}
	
	public JSONObject getWeFansListJson(WxPublic wePublic, String nextOpenId) {
		String fdCode = "wx359601ac8fa2a57a"; //wePublic.getQrcode();
		String appId = "wx359601ac8fa2a57a";//wePublic.getAppId();
		String appSecret = "b8b9dccc95af7cbf88cfa16e90c2ceaf";//wePublic.getAppSecret();
		AccessToken accessToken = WxUtils.getAccessToken4GZH(appId,appSecret);
//		String tokenId = TokenUtils.checkToken(fdCode, appId, appsecret,"test");
		String access_userlist_url = API4GZH.ACCESS_URSERLIST_URL;

		// 第一个拉取的OPENID，不填默认从头开始拉取
		String requestUrl = "";
		access_userlist_url = access_userlist_url.replace("ACCESS_TOKEN",accessToken.getToken());
		if (!StringUtils.isStringEmpty(nextOpenId)) {
			access_userlist_url = access_userlist_url.replace("next_openid",nextOpenId);
		} else {
			int index = access_userlist_url.indexOf("&");
			access_userlist_url = access_userlist_url.substring(0, index);
		}
		requestUrl = access_userlist_url;
		JSONObject jsonObject = WxUtils.httpRequest(requestUrl, "GET", null);
		return jsonObject;
	}

	private static Menu getMenu() {
		MainButton btn11 = new MainButton();
		btn11.setName("我的收藏");
		btn11.setType("click");
		btn11.setKey("BTN_FAV");
		
		MainButton btn21 = new MainButton();
		btn21.setName("刻下来的幸福");
		btn21.setType("click");
		btn21.setKey("BTN_HAPPY");
		
		MainButton btn22 = new MainButton();
		btn22.setName("我们的视频");
		btn22.setType("view");
		btn22.setUrl("http://223.4.182.163/i1510_base/wx/login.jsp");
		
		MainButton btn23 = new MainButton();
		btn23.setName("给我们留言");
		btn23.setType("view");
		btn23.setUrl("http://223.4.182.163/i1510_base/wx/login.jsp");
		
		MainButton btn24 = new MainButton();
		btn24.setName("扫码带提示");
		btn24.setType("scancode_waitmsg");
		btn24.setKey("rselfmenu_0_0");
		

		MainButton btn25 = new MainButton();
		btn25.setName("扫码推事件");
		btn25.setType("scancode_push");
		btn25.setKey("rselfmenu_0_1");

		SubButton subBtn2 = new SubButton();
		subBtn2.setName("波&娜"); 
		subBtn2.setSub_button(new MainButton[] {btn25,btn24,btn23,btn22,btn21 });
		
		MainButton btn33 = new MainButton();
		btn33.setName("系统拍照发图");
		btn33.setType("pic_sysphoto");
		btn33.setKey("rselfmenu_1_0");

		MainButton btn34 = new MainButton();
		btn34.setName("拍照或者相册发图");
		btn34.setType("pic_photo_or_album");
		btn34.setKey("rselfmenu_1_1");

		MainButton btn35 = new MainButton();
		btn35.setName("微信相册发图");
		btn35.setType("pic_weixin");
		btn35.setKey("rselfmenu_1_2");

		MainButton btn36 = new MainButton();
		btn36.setName("发送位置");
		btn36.setType("location_select");
		btn36.setKey("rselfmenu_2_0");
		
		MainButton btn37 = new MainButton();
		btn37.setName("软件版权");
		btn37.setType("view");
		btn37.setUrl("http://223.4.182.163/i1510_base");
		
		SubButton subBtn3 = new SubButton();
		subBtn3.setName("联系方式"); 
		subBtn3.setSub_button(new MainButton[] {btn37,btn33,btn34,btn35,btn36});

		/*CommonButton btn32 = new CommonButton();
		btn32.setName("投诉建议");
		btn32.setType("view");
		btn32.setUrl("http://mobiportal.cn/site/mobiportal/feedback");

		CommonButton btn33 = new CommonButton();
		btn33.setName("关于");
		btn33.setType("view");
		btn33.setUrl("http://mobiportal.cn/site/mobiportal/about");*/

		/*
		 * ComplexButton mainBtn1 = new ComplexButton();
		 * mainBtn1.setName("进入系统"); mainBtn1.setSub_button(new CommonButton[] {
		 * btn11 });
		 * 
		 * ComplexButton mainBtn2 = new ComplexButton();
		 * mainBtn2.setName("联盟推广"); mainBtn2.setSub_button(new CommonButton[] {
		 * btn21 });
		 */
	/*	ComplexButton mainBtn3 = new ComplexButton();
		mainBtn3.setName("系统助手");
		mainBtn3.setSub_button(new CommonButton[] { btn31, btn32, btn33 });*/

		/**
		 * 在某个一级菜单下没有二级菜单的情况，menu这样定义 menu.setButton(new Button[] { mainBtn1,
		 * mainBtn2, btn33 });
		 */
		Menu menu = new Menu();
		menu.setButton(new Button[] { btn11, subBtn2,subBtn3 });

		return menu;
	}
	public static void main(String[] args) {
		// 第三方用户唯一凭证
		String appId = "wx5ee543a74906af73";
		// 第三方用户唯一凭证密钥
		String appSecret = "cf62eb7c40aaea883dbbcabdbdcb0600";

		// 调用接口获取access_token
		AccessToken at = WxUtils.getAccessToken4GZH(appId, appSecret);

		if (null != at) {
			// 调用接口创建菜单
			int result = WxUtils.createMenuGZH(getMenu(), at.getToken());

			// 判断菜单创建结果
			if (0 == result)
				// log.info("菜单创建成功！");
				System.out.println("菜单创建成功！");
			else
				// log.info("菜单创建失败，错误码：" + result);
				System.out.println("菜单创建失败，错误码：" + result);
		}
	}
}
